package com.paisheng.justlife.utils;

import java.math.BigDecimal;

public class SubSrtingUtils {


    /**
     * 校验重量返回的串口
     * @param value
     * @return
     */
    public static String getStringVerify(String value) {
        String substring = value.substring(0, value.length()-4);
        byte[] bOutArray = MyFunc.HexToByteArr(substring);
        int crc16 = CRCUtils.calcCrc16(bOutArray, bOutArray.length);
        String replace = substring + String.format("0x%04x", crc16).replace("0x", "").toUpperCase();
        return replace;
    }

    /**
     * 计算公斤重量
     * @param value
     * @return
     */
    public static String getWeigh(String value) {
        int hexToInt = MyFunc.HexToInt(value.substring(10, 14));
        Float f = Float.valueOf(hexToInt)*10/1000;
        BigDecimal bigDecimal = new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_UP);
        return bigDecimal.toString();
    }

    /**
     * 总价格
     * @param weigh
     * @param plasticPrice
     * @return
     */
    public static String  getTotalmoney(String weigh, String plasticPrice) {
        return new BigDecimal(weigh).multiply(new BigDecimal(plasticPrice)).setScale(2,BigDecimal.ROUND_DOWN).toString();
    }


    public static int parseHex4(String value) {
        String num = value.substring(10, 14);
        if (num.length() != 4) {
            throw new NumberFormatException("Wrong length: " + num.length() + ", must be 4.");
        }
        int ret = Integer.parseInt(num, 16);
        ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
        return  ret;
    }

    /**
     * Byte转Bit
     */
    public static String byteToBit(byte b) {
        return "" +(byte)((b >> 7) & 0x1) +
                (byte)((b >> 6) & 0x1) +
                (byte)((b >> 5) & 0x1) +
                (byte)((b >> 4) & 0x1) +
                (byte)((b >> 3) & 0x1) +
                (byte)((b >> 2) & 0x1) +
                (byte)((b >> 1) & 0x1) +
                (byte)((b >> 0) & 0x1);
    }
    /**
     * Bit转Byte
     */
    public static byte BitToByte(String byteStr) {
        int re, len;
        if (null == byteStr) {
            return 0;
        }
        len = byteStr.length();
        if (len != 4 && len != 8) {
            return 0;
        }
        if (len == 8) {// 8 bit处理
            if (byteStr.charAt(0) == '0') {// 正数
                re = Integer.parseInt(byteStr, 2);
            } else {// 负数
                re = Integer.parseInt(byteStr, 2) - 256;
            }
        } else {//4 bit处理
            re = Integer.parseInt(byteStr, 2);
        }
        return (byte) re;
    }


    public static String toBinaryString(int num) {

        char[] chs = new char[16];
        for(int i = 0; i < 16; i++) {
            chs[16 - 1 - i] = (char)((num >> i & 1) + '0');
        }
        return new String(chs);
    }


    /**
     * 获取外设状态
     * @param potr
     * @return
     */
    public static String getPeripheralStatusNum(String potr){
        String potrStr = potr.substring(36, 40);
        int hexToInt = MyFunc.HexToInt(potrStr);
        return SubSrtingUtils.toBinaryString(hexToInt);
    }
    /**
     * 获取外设箱号
     * @param sMsg
     * @return
     */
    public static String getBoxNumber(String sMsg){
        return sMsg.substring(9, 10);
    }


    /**
     * 获取下仓门关闭值，1是开，0是关
     * @param potr
     * @return
     */
    public static String getBelowDoorClose(String potr){
        String statusNum = getPeripheralStatusNum(potr);
        return String.valueOf(statusNum.charAt(statusNum.length()-1));
    }

}
